<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%
  String path = request.getContextPath();
%>
<jsp:include page="/pages/commons/top.jsp" />
<jsp:include page="/pages/commons/left.jsp" />

<div data-options="region:'center',title:'部门管理',iconCls:'icon-ok'">  
	<table id="dg" title="部门列表" class="easyui-treegrid" style="width: 1000px; height: 477px" url="<%=path %>/dept/getTreeList.do?id=0" toolbar="#toolbar" animate="true" treeField='deptName' idField="id" fitColumns="true" method="post" singleSelect="false" loadMsg="数据载入中...">
		<thead>
			<tr>
			    <th data-options="field:'ck',checkbox:true"></th> 
				<th field="deptName" width="80">部门名称</th>
				<th field="createTimeStr" width="50">创建日期</th>
			</tr>
		</thead>
	</table>
	<div id="toolbar" style="padding:5px;height:auto">  
        <div style="margin-bottom:5px">  
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newDept()">添加部门</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editDept()">修改部门</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delDept()">删除部门</a>
		</div>
	</div>

	<div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px" closed="true" buttons="#dlg-buttons">
		<div class="ftitle">部门信息</div>
		<form id="fm" method="post" novalidate>
			<div class="fitem">
				<label>部门名称：</label> <input id="deptNameObj" name="deptName" class="easyui-validatebox" required="true" validType="length[0,100]"/>
			</div>
            <input name="id" id="idObj" value="" type="hidden" />

		</form>
	</div>
	<div id="dlg-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveDept()">保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
	</div>
</div>

<jsp:include page="/pages/commons/footer.jsp" />


<script type="text/javascript">  
        var url;
        var parentDeptId = 0;
        function newDept(){  
        	var row = $('#dg').datagrid('getSelected'); 
        	if (row){ 
        		parentDeptId = row.id; 
        	}
            $('#dlg').dialog('open').dialog('setTitle','添加部门');  
            $('#fm').form('clear'); 
            url = "<%=path%>/dept/add.do";  
        } 
		
        

        function editDept(){  
            var row = $('#dg').datagrid('getSelected');  
            if (row){  
                $('#dlg').dialog('open').dialog('setTitle','修改部门');  
                $('#fm').form('load',row);
                parentDeptId = row.parentDeptId; 
                url = "<%=path%>/dept/mod.do";
            }  
        }  
        
       
        function saveDept(){ 
            var deptName = $("#deptNameObj").val();
            
			if($('#fm').form('validate')){
			    var data = "deptName="+deptName+"&parentDeptId="+parentDeptId;
			    if($("#id").val()!=''){
			    	data += "&id="+$("#idObj").val();
	            }
			    $.post(url,data,saveCallBack);  
			}
        } 

        function saveCallBack(result){
            var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
            }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dlg').dialog('close');      // close the dialog  
                $('#dg').treegrid('reload');
    	        return true;
            } 
        }	
 
        function delDept(){  
            var row = $('#dg').datagrid('getSelections');  
            if (row.length > 0){  
                $.messager.confirm('Confirm','确定删除该部门吗?',function(r){  
                    if (r){  
						var data = "";
						for(var i=0;i<row.length;i++){
						    data += row[i].id
						    if(i<row.length - 1){
						        data+=",";
							}
						}
                    	$.post("<%=path%>/dept/del.do",{'ids':data},delCallBack);   
                    }  
                });  
            }  
        } 
        
        function delCallBack(result){
        	var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
	        }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dg').treegrid('reload');
    	        return true;
            } 
        } 
       

    </script>
<style type="text/css">
#fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}
</style>

</html>
